$(function() {
    $("select").comboSelect();
    if (view_sql) {
        $('#sql_text').modal();
    }

    $('[data-toggle="tooltip"]').tooltip()

    $("#change_testor").click(function() {
        if ($("#select_testor").val() == $('#origin_testor').html()) {
            alert('测试人员无变化');
            return false;
        }
        if (confirm("是否确更换测试人员？")) {
            var url = "/gitool/change_testor_ajax";
            $.post(url, {branch_id: $(this).data('branch_id'), testor: $("#select_testor").val()}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }

            }, 'json');
            return false;
        }
    });

    $("#change_push_engineer").click(function() {
        if ($("#select_push_engineer").val() == $('#origin_push_engineer').html()) {
            alert('上线人员无变化');
            return false;
        }
        if (confirm("是否确更换上线人员？")) {
            var url = "/gitool/change_push_engineer_ajax";
            $.post(url, {branch_id: $(this).data('branch_id'), push_engineer: $("#select_push_engineer").val()}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }

            }, 'json');
            return false;
        }
    });

    $("#edit_task").click(function() {
        location.href = "/gitool/edit_task?branch_id="+$(this).data('branch_id');
    });

    $('#close_task').click(function() {
        if (confirm("是否确认关闭分支？（此过程不可逆）")) {
            var url = "/gitool/close_task_ajax";
            $.post(url, {branch_id: $(this).data('branch_id')}, function(data) {
                if(data.status == 'success') {
                    location.href="/gitool/my_task_list"
                } else {
                    alert(data.message);
                }

            }, 'json');
            return false;
        }
    });

    $("#start_test").click(function () {
        if (confirm("是否确认开始测试？")) {
            var url = "/gitool/start_test_ajax";
            $.post(url, {branch_id: $(this).data('branch_id')}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }

            }, 'json');
            return false;
        }
    });

    $("#switch_test_env").click(function() {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);

        var url = "/gitool/switch_test_env_ajax";
        $.post(url, {branch_id: $(this).data('branch_id'), test_env_id: $("#test_env").val()}, function(data) {
            if(data.status == 'success') {
                showModal(data.message);
            } else {
                alert(data.message);
            }
            buttonRecover(this_obj);
        }, 'json');
        return false;
    });

    $("#update_test_env").click(function() {

        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);

        var url = "/gitool/update_test_env_ajax";
        $.post(url, {branch_id: $(this).data('branch_id'), commit_id:$('#commit_id').val()}, function(data) {
            if(data.status == 'success') {
                showModal(data.message);
            } else {
                alert(data.message);
            }
            buttonRecover(this_obj);
        }, 'json');
        return false;
    });

    $("#test_pass").click(function () {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        testResultSubmit(this_obj, function (this_obj) {
            var url = "/gitool/update_test_pass_ajax";
            var test_result_remark = $('#test_result_remark_content').val();
            $.post(url, {branch_id: this_obj.data('branch_id'), test_result_remark: test_result_remark}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }
            }, 'json');
            return false;
        });
        return false;
    });

    $("#test_failed").click(function () {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        testResultSubmit(this_obj, function (this_obj) {
            var url = "/gitool/update_test_failed_ajax";
            var test_result_remark = $('#test_result_remark_content').val();
            $.post(url, {branch_id: this_obj.data('branch_id'), test_result_remark: test_result_remark}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }
            }, 'json');
            return false;
        });
        return false;
    });


    $("#release_pre").click(function () {
        if (confirm("是否确认切到预发布环境？")) {

            var url = "/gitool/release_pre_env_ajax";
            var branch_id = $(this).data('branch_id');
            var $modal = $('#my_modal');
            var $modal_content = $('#my_modal_content');
            var $tips = $('#my_modal_tips');

            $('#myModalLabel').html("操作进度");
            $modal_content.removeClass('terminal');
            $('#my_modal').modal({
                keyboard: false,
                backdrop: 'static'
            });

            $close = $('.my_modal_close');
            $close.hide();

            var $desc = $('#progress_desc');

            setMultiApiList(branch_id, 'release_pre', $desc);

            multiSyncAjax(url, branch_id, $desc, 1, $desc.children().length, '发布预上线环境成功，请关闭窗口');

            return false;
        }
    });

    $("#start_pre_test").click(function() {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        if (confirm("是否确认开始预上线环境测试？")) {
            var url = "/gitool/start_pre_test_ajax";
            $.post(url, {branch_id: $(this).data('branch_id')}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                    buttonRecover(this_obj);
                }

            }, 'json');
            return false;
        } else {
            buttonRecover(this_obj);
        }
    });

    $("#pre_test_pass").click(function() {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        testResultSubmit(this_obj, function (this_obj) {
            var url = "/gitool/update_pre_test_pass_ajax";
            var test_result_remark = $('#test_result_remark_content').val();
            $.post(url, {branch_id: this_obj.data('branch_id'), test_result_remark: test_result_remark}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }
            }, 'json');
            return false;
        });
        return false;
    });

    $("#pre_test_failed").click(function() {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        testResultSubmit(this_obj, function (this_obj) {
            var url = "/gitool/update_pre_test_failed_ajax";
            var test_result_remark = $('#test_result_remark_content').val();
            $.post(url, {branch_id: this_obj.data('branch_id'), test_result_remark: test_result_remark}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                }
            }, 'json');
            return false;
        });
        return false;
    });

    $("#release_online").click(function () {
        if (confirm("是否确认上线？？？")) {

            var url = "/gitool/release_online_ajax";
            var branch_id = $(this).data('branch_id');
            var $modal = $('#my_modal');
            var $modal_content = $('#my_modal_content');
            var $tips = $('#my_modal_tips');

            $('#myModalLabel').html("操作进度");
            $modal_content.removeClass('terminal');
            $('#my_modal').modal({
                keyboard: false,
                backdrop: 'static'
            });

            $close = $('.my_modal_close');
            $close.hide();

            var $desc = $('#progress_desc');

            setMultiApiList(branch_id, 'release_online', $desc);

            multiSyncAjax(url, branch_id, $desc, 1, $desc.children().length, '上线成功，请关闭窗口');

            return false;
        }
    });

    $("#rollback_online").click(function () {
        if (confirm("是否确认回滚分支？？？")) {

            var url = "/gitool/rollback_online_ajax";
            var branch_id = $(this).data('branch_id');
            var $modal = $('#my_modal');
            var $modal_content = $('#my_modal_content');
            var $tips = $('#my_modal_tips');

            $('#myModalLabel').html("操作进度");
            $modal_content.removeClass('terminal');
            $('#my_modal').modal({
                keyboard: false,
                backdrop: 'static'
            });

            $close = $('.my_modal_close');
            $close.hide();

            var $desc = $('#progress_desc');

            setMultiApiList(branch_id, 'rollback_online', $desc);

            multiSyncAjax(url, branch_id, $desc, 1, $desc.children().length, '回滚成功，请关闭窗口');

            return false;
        }
    });

    $('.sql_done').click(function() {
        var this_obj = $(this);
        if (buttonIsDisabled(this_obj)) {
            return false;
        }
        buttonDisable(this_obj);
        if (confirm("是否确认"+$(this).html()+"？")) {
            var url = "/gitool/update_sql_done_ajax";
            var action = $(this).data('action');
            $.post(url, {branch_id: $(this).data('branch_id'), action: action}, function(data) {
                if(data.status == 'success') {
                    location.reload();
                } else {
                    alert(data.message);
                    buttonRecover(this_obj);
                }

            }, 'json');
            return false;
        } else {
            buttonRecover(this_obj);
        }
        
    });

    $('#my_modal').on('hidden.bs.modal', function (e) {
        location.reload();
    });

    function setMultiApiList (branch_id, action, $desc) {
        var url = '/gitool/get_multi_api_list';
        $.ajax({url:url, data:{branch_id: branch_id, action: action}, async:false, type:"POST", dataType:"json", success:function(ret) {
            $.each(ret.data, function (k,v) {
                $desc.append('<p class="text-primary" style="display: none;"><span>'+v+'</span>...<b style="margin-left:20px"><img src="'+wait_gif+'"/></b></p>');
            });
        }});
        $desc.children().eq(0).show();
    }

    function multiSyncAjax (url, branch_id, $desc, step, total_step, success_msg) {
        if (step > total_step) {
            return false;
        }

        $.ajax({
            url: url,
            type: 'GET',
            dataType: 'json',
            data: {
                step : step,
                branch_id: branch_id
            },
            async:'false',
            success:function(data)  {
                if (data.message) {
                    showTerminal($desc.children().eq(step-1).find('span').html(), data.message);
                }
                if(data.status == 'success') {
                    if (step == total_step) {
                        $("#progress").css('width', '100%');
                        $desc.children().eq(step-1).find('b').html('Done');
                        $('.my_modal_close').show();
                        $('#my_modal_tips').html(success_msg);
                        $('#my_modal_tips').show('100');
                        
                    } else {
                        $("#progress").css('width', (step/total_step*100)+'%');
                        $desc.children().eq(step-1).find('b').html('Done');
                        $desc.children().eq(step).show();
                        multiSyncAjax(url, branch_id, $desc, step+1, total_step, success_msg);
                    }
                } else {
                    showModalError(data.message);
                    $('.my_modal_close').show();
                    return false;
                }
            },
            error:function(data) {
                console.log("ajax 调用出错"+data);          
            }
        });    
    }

    function testResultSubmit (obj, callback) {
        $('#test_result_remark_title').html(obj.html() + '-备注');
        $('#test_result_remark_content').val('');
        $('#test_result_remark').modal();
        buttonRecover(obj);
        $('#test_result_remark_submit').unbind();
        $('#test_result_remark_submit').click(function () {
            if (!$('#test_result_remark_content').val()) {
                alert('一定要写备注哦~');
                return false;
            }
            if (buttonIsDisabled($(this))) {
                return false;
            }
            buttonDisable($(this));
            callback(obj);
        });
    }

    function showModal (content) {
        $('#my_modal_content').html(content);
        $('#my_modal').modal();
    }

    function showModalError (content, classname) {
        $tips = $('#my_modal_tips');
        $tips.removeClass('alert-success');
        if (classname) {
            $tips.addClass(classname);
        } else {
            $tips.addClass('alert-danger');
        }

        $tips.html(content);
        $tips.show('100');
    }

    function showTerminal (title, terminal_msg) {
        $terminal = $('#terminal');
        title = '------------ ' + title + ' ------------<br />';
        var old = $terminal.html();
        if (old) {
            old = old + '<br /><br />';
        }
        $terminal.html(old+title+terminal_msg);
        $terminal.show();
    }

    function buttonIsDisabled (obj) {
        if (obj.hasClass('disabled')) {
            return true;
        }
        return false;
    }

    function buttonDisable (obj) {
        obj.addClass('disabled');
    }

    function buttonRecover (obj) {
        obj.removeClass('disabled');
    }

});